package com.wisdudu.blank.ui.login;

import android.content.Intent;
import android.databinding.ObservableField;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.bean.UserInfo;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextErrorListener;
import com.wisdudu.blank.databinding.FragmentPasSetBinding;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;
import com.wisdudu.blank.ui.user.UserInfoSetActivity;
import com.wisdudu.frame.support.util.RegularUtil;

/**
 * Created by ypp on 2016/10/19.
 */
public class PasSetVM implements ViewModel {

    public ObservableField<String> password = new ObservableField<>();
    public ObservableField<String> rePassword = new ObservableField<>();
    public ObservableField<String> phone = new ObservableField<>();
    private PasSetFragment mFragment;
    private FragmentPasSetBinding mBinding;
    private UserRepo mUserRepo;
    private String code;

    public PasSetVM(PasSetFragment mFragment, FragmentPasSetBinding mBinding, String phone, String code) {
        this.mBinding = mBinding;
        this.mFragment = mFragment;
        mUserRepo = Injection.provideUserRepo();
        this.phone.set(phone);
        this.code = code;
    }

    public ReplyCommand step3OnClick = new ReplyCommand(() -> {
        toRegister();
    });

    private void toRegister() {
        if (TextUtils.isEmpty(password.get())) {
            ToastUtil.INSTANCE.toast("密码不能为空");
            return;
        }
        if (!RegularUtil.isPassword(password.get())) {
            ToastUtil.INSTANCE.toast("请输入6-20位密码");
            return;
        }
        if (TextUtils.isEmpty(rePassword.get())) {
            ToastUtil.INSTANCE.toast("重复密码不能为空");
            return;
        }
        if (!rePassword.get().equals(password.get())) {
            ToastUtil.INSTANCE.toast("两次密码输入不一致");
            return;
        }
        mUserRepo.register((new ProgressSubscriber(new SubscriberOnNextErrorListener<UserInfo>() {

            @Override
            public void onNext(UserInfo userInfo, LoadingProgressDialog pd) {
                ToastUtil.INSTANCE.toast("注册成功");
                String uid = userInfo.getUid();
                Intent intent = new Intent(mFragment.getActivity(), UserInfoSetActivity.class);
                intent.putExtra("uid", uid);
                mFragment.startActivity(intent);
                mFragment.getActivity().finish();
            }

            @Override
            public void onError(Throwable e, LoadingProgressDialog pd) {
                ToastUtil.INSTANCE.toast(e.getMessage());
            }
        }, mFragment.getActivity(), "正在提交...")), phone.get(), rePassword.get(), code);
    }

}
